package model

import (
	"github.com/lab-online/internal/report/entity"
	"github.com/lab-online/internal/shared/valueobject"
	"github.com/lab-online/pkg/model"
)

type Report struct {
	model.BaseModel
	ClassID      uint    `gorm:"index"`
	ExperimentID uint    `gorm:"index"`
	Student      string  `gorm:"index,type:varchar(8)"`
	Score        float32 `gorm:""`
	Status       uint    `gorm:""`
	Content      Content `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
}

func (r *Report) ToEntity() entity.ReportEntity {
	return &entity.Report{
		Report: valueobject.Report{
			ID:           r.ID,
			ClassID:      r.ClassID,
			ExperimentID: r.ExperimentID,
			Student:      r.Student,
			Score:        r.Score,
			Status:       r.Status,
			Content:      r.Content.Content,
		},
	}
}

type ReportOption func(*Report)

func New(opts ...ReportOption) *Report {
	report := &Report{}
	for _, opt := range opts {
		opt(report)
	}
	return report
}

func WithID(id uint) ReportOption {
	return func(r *Report) {
		r.ID = id
	}
}

func WithClassID(classID uint) ReportOption {
	return func(r *Report) {
		r.ClassID = classID
	}
}

func WithExperimentID(experimentID uint) ReportOption {
	return func(r *Report) {
		r.ExperimentID = experimentID
	}
}

func WithStudent(student string) ReportOption {
	return func(r *Report) {
		r.Student = student
	}
}

func WithContent(content Content) ReportOption {
	return func(r *Report) {
		r.Content = content
	}
}

func WithEntity(e entity.ReportEntity) ReportOption {
	return func(r *Report) {
		r.ID = e.GetID()
		r.ClassID = e.GetClassID()
		r.ExperimentID = e.GetExperimentID()
		r.Student = e.GetStudent()
		r.Status = e.GetStatus()
		r.Score = e.GetScore()
		r.Content = Content{
			ReportID: e.GetID(),
			Content:  e.GetContent(),
		}
	}
}
